<style scope lang="less">
.tab-knowledge {
  width: calc(100% - 20px);
  height: calc(100% - 20px);
  min-width: 1024px;
  margin-top: 10px;
  background-color: white;
  font-size: 14px;
  .toolbar {
    display: flex;
    padding-top: 10px;
    padding-bottom: 10px;
    padding-left: 20px;
    .class-btn {
      padding: 3px 20px 3px 20px;
      background-image: url(../../../assets/images/class-btn-unselect.png);
      background-repeat: no-repeat;
      background-size: 15px 80%;
      background-position: center right;
      cursor: pointer;
    }
    .class-btn-active {
      background-image: url(../../../assets/images/class-btn-select.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      margin: 0px 0px 0px 20px;
      color: white;
    }
    .class-title {
      padding: 3px 80px 3px 15px;
    }
    .more {
      padding: 3px 20px 3px 15px;
      margin-left: auto;
      cursor: pointer;
      color: #b3b3b3;
    }
  }
  .card-list {
    padding: 10px 50px !important;
    box-shadow: none !important;
  }
  .list {
    margin-top: 20px !important;
  }
}
</style>

<template>
	<div class="tab-knowledge">
		<div class="toolbar">
			<div
				:class="{'class-btn-active': currentKnowledgeClass === item.name || (index === 0 && !currentKnowledgeClass), 'class-btn': true}"
				:key="item.name"
				@click="selectKnowledgeClass(item.name)"
				v-for="(item, index) in knowledgeType"
			>{{item.name}}</div>
		</div>
		<card-list
			:list="documents"
			:on-card-click="documentCardClick"
			:on-page-change="documentPageChange"
			:on-search-click="documentSearch"
			:total="documentsTotal"
			v-show="currentKnowledgeClass === '学习文档'"
		/>
		<card-list
			:list="videos"
			:on-card-click="videoCardClick"
			:on-page-change="videoPageChange"
			:on-search-click="videoSearch"
			:total="videosTotal"
			v-show="currentKnowledgeClass === '操作视频'"
		/>
		<preview-pdf ref="docment"/>
		<preview-video ref="video"/>
	</div>
</template>

<script>
import { mapGetters, mapActions, mapMutations } from "vuex";

import cardList from "@/components/cardList/cardList";
import previewPDF from "@/components/preview/previewPDF";
import previewVideo from "@/components/preview/previewVideo";

export default {
  name: "tab-knowledge",
  components: {
    "preview-pdf": previewPDF,
    "preview-video": previewVideo,
    "card-list": cardList
  },
  data() {
    return {
      cardList: [],
      currentKnowledgeClass: "学习文档",
      knowledgeType: [{ name: "学习文档" }, { name: "操作视频" }]
    };
  },
  created() {
    this.loadData();
  },
  computed: {
    ...mapGetters(["documents", "videos", "documentsTotal", "videosTotal"])
  },
  methods: {
    ...mapActions(["getDocuments", "getVideos"]),
    loadData() {
      let initPage = {
        loading: false,
        size: 100,
        number: 0,
        filter: {
          enabled: true
        }
      };
      this.getDocuments(initPage);
      this.getVideos(initPage);
    },

    // 文档搜索
    documentSearch(keyword) {
      let params = {
        size: 4,
        number: 0,
        filter: {
          title: keyword
        }
      };
      this.getDocuments(params);
    },

    // 视频搜索
    videoSearch(keyword) {
      let params = {
        size: 4,
        number: 0,
        filter: {
          title: keyword
        }
      };
      this.getVideos(params);
    },

    // 知识类型选择
    selectKnowledgeClass(name) {
      this.currentKnowledgeClass = name;
    },
    // 文件
    documentCardClick(row) {
      this.$refs.docment.show(`api/${row.contentFile}`);
    },
    documentPageChange(page) {
      this.getDocuments({
        size: 4,
        number: page
      });
    },

    // 视频
    videoCardClick(row) {
      this.$refs.video.show(`api/${row.contentFile}`);
    },
    videoPageChange(page) {
      this.getVideos({
        size: 4,
        number: page
      });
    }
  }
};
</script>
